package paperfly.input.ajust;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class CaptureColorsPanel extends JPanel {
	private static final long serialVersionUID = 3107868877447336694L;
	private JLabel camPicture;
	private JLabel labelAsaDireita;
	private JLabel labelAsaEsquerda;
	private JLabel labelCorDireita;
	private JLabel labelCorEsquerda;
	private JRadioButton botaoSelecionarDireita;
	private JRadioButton botaoSelecionarEsquerda;
	private JLabel labelInstrucoes;
	private InputCalibration parent;
	private BufferedImage camImage;
	private boolean selected;
	
	public CaptureColorsPanel(InputCalibration inputCalibration) {
		init(inputCalibration);
		adjustPositions();
		setUpListeners();
	}

	private void init(InputCalibration calibration) {
		this.parent = calibration;
		
		selected = false;
		camPicture = new JLabel();
		labelAsaDireita = new JLabel("Asa Direita:");
		labelAsaEsquerda = new JLabel("Asa Esquerda: ");
		labelCorDireita = new JLabel();
		labelCorEsquerda = new JLabel();
		botaoSelecionarDireita = new JRadioButton("Selecionar",null, true);
		botaoSelecionarEsquerda = new JRadioButton("Selecionar");
		ButtonGroup grupo = new ButtonGroup();
		grupo.add(botaoSelecionarDireita);
		grupo.add(botaoSelecionarEsquerda);

		labelInstrucoes = new JLabel(
				"Passe o mouse sobre a imagem e defina as cores que devem ser capturadas pelo jogo.");

		labelCorDireita.setPreferredSize(new Dimension(50, 25));
		labelCorEsquerda.setPreferredSize(new Dimension(50, 25));
		labelCorDireita.setBackground(Color.black);
		labelCorEsquerda.setBackground(Color.black);
		labelCorDireita.setOpaque(true);
		labelCorEsquerda.setOpaque(true);
	}

	private void adjustPositions() {
		JPanel painelAsaDireita = new JPanel();
		painelAsaDireita.add(labelAsaDireita);
		painelAsaDireita.add(labelCorDireita);
		painelAsaDireita.add(botaoSelecionarDireita);
		painelAsaDireita.setBorder(BorderFactory.createBevelBorder(1));
		JPanel painelAsaEsquerda = new JPanel();
		painelAsaEsquerda.add(labelAsaEsquerda);
		painelAsaEsquerda.add(labelCorEsquerda);
		painelAsaEsquerda.add(botaoSelecionarEsquerda);

		JPanel painel = new JPanel();
		painel.setLayout(new BorderLayout(5, 5));
		painel.add(labelInstrucoes, BorderLayout.NORTH);
		painel.add(painelAsaDireita, BorderLayout.CENTER);
		painel.add(painelAsaEsquerda, BorderLayout.SOUTH);

		setLayout(new BorderLayout(5, 5));
		add(camPicture, BorderLayout.CENTER);
		add(painel, BorderLayout.SOUTH);
	}

	private void setUpListeners() {
		camPicture.addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				if (camPicture.getIcon() != null && !selected) {
					int camWidth = camImage.getWidth();
					int pictreWidth = camPicture.getWidth();
					int camHeight = camImage.getHeight();
					int pictreHeight = camPicture.getHeight();

					int xCam = (camWidth * e.getX()) / pictreWidth;
					int yCam = (camHeight * e.getY()) / pictreHeight;
					int pixelColor;
					try {
						pixelColor = camImage.getRGB(xCam, yCam);
						int r = (pixelColor >> 16) & 255;
						int g = (pixelColor >> 8) & 255;
						int b = pixelColor & 255;
						Color cor = new Color(r, g, b);
						if(botaoSelecionarDireita.isSelected()){
							labelCorDireita.setBackground(cor);
						}
						else{
							labelCorEsquerda.setBackground(cor);
						}
					} catch (RuntimeException e1) {
						System.out.println(xCam + "," + yCam);
					}
				}
			}
		});
		camPicture.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				selected = !selected;
			}
		});
		ActionListener selectListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selected = false;
			}
		};
		botaoSelecionarDireita.addActionListener(selectListener);
		botaoSelecionarEsquerda.addActionListener(selectListener);
	}

	public void setCamImage(BufferedImage camImage) {
		this.camImage = camImage;
		int camWidth = camPicture.getWidth();
		int camHeight = camPicture.getHeight();
		camPicture.setIcon(new ImageIcon(camImage.getScaledInstance(camWidth, camHeight, 0)));
	}
}
